//
//  NSDate+Extension.swift
//  ThreeStone
//
//  Created by 王磊 on 10/16/16.
//  Copyright © 2016 ThreeStone. All rights reserved.
//

import UIKit

extension NSDate {
    
    internal static func timeStamp() -> Int {
        return Int(NSDate().timeIntervalSince1970)
    }
    internal static func dateStamp(date: NSDate) -> String {
        
        return "\(Int(date.timeIntervalSince1970))"
    }
    
    internal static func timeStamp_ms() -> Int {
        
        return Int(NSDate().timeIntervalSince1970) * 1000
    }
}
private let calendar: NSCalendar = NSCalendar.currentCalendar()

private let fmt: NSDateFormatter = NSDateFormatter()

extension NSDate {
    
    // 根据时间戳 确定时间
    internal static func dateFormString(dateStamp: String = "") -> (TSDateType /* 日期类型*/,String /* 去年 几年前*/,String/* yyyy-MM-dd*/,String/* yyyy-MM-dd HH:mm*/) {
        
        fmt.dateFormat = "yyyy-MM-dd HH:mm"
        
        let format = NSDateFormatter()
        
        format.dateFormat = "yyyy-MM-dd HH:mm"
        
        if dateStamp != "" {
            let date = NSDate(timeIntervalSince1970: Double(dateStamp)!)
            
            let nowDate = NSDate()
            
            let unit: NSCalendarUnit = NSCalendarUnit(rawValue: NSCalendarUnit.Day.rawValue | NSCalendarUnit.Month.rawValue | NSCalendarUnit.Year.rawValue | NSCalendarUnit.Minute.rawValue | NSCalendarUnit.Second.rawValue | NSCalendarUnit.Hour.rawValue)
            //设置格式
            let nowCom = calendar.components(unit, fromDate: nowDate)
            
            let timeCom = calendar.components(unit, fromDate: date)
            
            if timeCom.year < nowCom.year  {
                
                return timeCom.year - nowCom.year == -1 ? (.LastYear ,"去年" ,fmt.stringFromDate(date),format.stringFromDate(date)): (.PreviousYear, "\(nowCom.year - timeCom.year)年前", fmt.stringFromDate(date),format.stringFromDate(date))
                
            } else if timeCom.year == nowCom.year {
                
                if timeCom.month < nowCom.month {
                    
                    return timeCom.month - nowCom.month == -1 ? (.LastMonth ,"上个月" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.PreviousMonth ,"\(nowCom.month - timeCom.month)个月前" ,fmt.stringFromDate(date),format.stringFromDate(date))
                    
                } else if timeCom.month == nowCom.month {
                    
                    if timeCom.day < nowCom.day {
                        
                        return timeCom.day - nowCom.day == -1 ? (.Yesterday ,"昨天" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.PreviousDay ,"\(nowCom.day - timeCom.day)天前" ,fmt.stringFromDate(date),format.stringFromDate(date))
                        
                    } else if timeCom.day == nowCom.day {
                        
                        if timeCom.hour < nowCom.hour {
                            
                            return timeCom.hour - nowCom.hour == -1 ? (.OwnHourAgo ,"一小时前" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.HoursAgo ,"\(nowCom.hour - timeCom.hour)小时前" ,fmt.stringFromDate(date),format.stringFromDate(date))
                            
                        } else if timeCom.hour == nowCom.hour {
                            
                            if timeCom.minute < nowCom.minute {
                                
                                return timeCom.hour - nowCom.hour < 5 ? (.Just ,"刚刚" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.HoursAgo ,"\(nowCom.hour - timeCom.hour)分钟前" ,fmt.stringFromDate(date),format.stringFromDate(date))
                                
                            } else if timeCom.minute == nowCom.minute {
                                
                                return (.JustNow ,"现在" ,fmt.stringFromDate(date),format.stringFromDate(date))
                            } else {
                                
                                return (.MinutesLater ,"\(nowCom.hour - timeCom.hour)分钟后" ,fmt.stringFromDate(date),format.stringFromDate(date))
                            }
                        } else {
                            
                            return timeCom.hour - nowCom.hour == 1 ? (.OwnHourLater ,"一小时后" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.HoursLater ,"\(timeCom.hour - nowCom.hour)小时后" ,fmt.stringFromDate(date),format.stringFromDate(date))
                        }
                    } else {
                        
                        return timeCom.day - nowCom.day == 1 ? (.Tomorrow ,"明天" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.FurtureDay ,"\(timeCom.day - nowCom.day)天后" ,fmt.stringFromDate(date),format.stringFromDate(date))
                    }
                    
                } else {
                    return timeCom.month - nowCom.month == 1 ? (.NextMonth ,"明天",fmt.stringFromDate(date),format.stringFromDate(date)) : (.FurtureMonth ,"\(timeCom.month - nowCom.month)月后",fmt.stringFromDate(date),format.stringFromDate(date))
                }
            } else {
                
                return timeCom.year - nowCom.year == 1 ? (.NextYear ,"明年" ,fmt.stringFromDate(date),format.stringFromDate(date)) : (.FurtureYear ,"\(timeCom.year - nowCom.year)月后" ,fmt.stringFromDate(date),format.stringFromDate(date))
            }
            
        } else {
            
            return (.Unknown ,"时间戳错误" ,"","")
        }
    }
}
extension NSDate {
    
    internal static func mdhm(dateStamp: String = "") -> String {
        
        let date = NSDate(timeIntervalSince1970: Double(dateStamp)!)
        
        fmt.dateFormat = "MM-dd HH:mm"
        
        return fmt.stringFromDate(date)
    }
}
extension NSDate {
    
    internal static func caculateAge(dateStamp: String = "") -> String {
        
        let birthday = NSDate(timeIntervalSince1970: Double(dateStamp)!)
        
        fmt.dateFormat = "yyyy"
        
        let brithdayStr = fmt.stringFromDate(birthday)
        
        let current = NSDate()
        
        let currentStr = fmt.stringFromDate(current)
        
        let birthdayInt = Int(brithdayStr)
        
        let currentInt = Int(currentStr)
        
        return "\(currentInt! - birthdayInt!)岁"
    }
    
    
}

